<?php namespace Phpcmf\Model\Bdts;

class Bdts extends \Phpcmf\Model
{

    private $zzurl = [
        'add' => 'http://data.zz.baidu.com/urls',
        'edit' => 'http://data.zz.baidu.com/update',
        'del' => 'http://data.zz.baidu.com/del',
    ];


    private $zzconfig;

    // 配置信息
    public function getConfig() {

        if ($this->zzconfig) {
            return $this->zzconfig;
        }

        if (is_file(WRITEPATH.'config/bdts.php')) {
            $this->zzconfig = require WRITEPATH.'config/bdts.php';
            return $this->zzconfig;
        }

        return [];
    }

    // 配置信息
    public function setConfig($data) {

        \Phpcmf\Service::L('Config')->file(WRITEPATH.'config/bdts.php', '站长配置文件', 32)->to_require($data);

    }

    // 进行百度推送
    public function module_bdts($mid, $url, $action = 'add') {

        $config = $this->getConfig();
        if (!$config) {
            return;
        } elseif (!$config['use']) {
            return;
        }

        $uri = parse_url($url);
        $site = $uri['host'];
        if (!$site) {
            return;
        }

        // 获取移动端域名
        $murl = dr_url_prefix('test.html', $mid, SITE_ID, 1);
        $uri = parse_url($murl);
        $m_site = $uri['host'];
        if ($m_site && $m_site != $site) {
            $murl = str_replace($site, $m_site, $url); // 替换移动端url
        } else {
            $m_site = '';
        }

        // 百度主动推送
        if ($config['bdts']) {
            $token = '';
            $m_token = '';
            foreach ($config['bdts'] as $t) {
                if ($t['site'] == $site && !$token) {
                    $token = $t['token'];
                }
                if ($m_site && $t['site'] == $m_site && !$m_token) {
                    $m_token = $t['token'];
                }
            }
            if ($token) {
                $api = $this->zzurl[$action].'?site='.$site.'&token='.$token;
                $urls = [$url];
                $ch = curl_init();
                $options =  array(
                    CURLOPT_URL => $api,
                    CURLOPT_POST => true,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_POSTFIELDS => implode("\n", $urls),
                    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
                );
                curl_setopt_array($ch, $options);
                $rt = json_decode(curl_exec($ch), true);
                if ($rt['error']) {
                    // 错误日志
                    @file_put_contents(WRITEPATH.'bdts_log.php', date('Y-m-d H:i:s').' ['.$url.'] - 失败 - '.$rt['message'].PHP_EOL, FILE_APPEND);
                } else {
                    // 推送成功
                    @file_put_contents(WRITEPATH.'bdts_log.php', date('Y-m-d H:i:s').' ['.$url.'] - 成功'.PHP_EOL, FILE_APPEND);
                }
            }
            if ($m_token && $m_site) {
                $api = $this->zzurl[$action].'?site='.$m_site.'&token='.$m_token;
                $urls = [$murl];
                $ch = curl_init();
                $options =  array(
                    CURLOPT_URL => $api,
                    CURLOPT_POST => true,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_POSTFIELDS => implode("\n", $urls),
                    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
                );
                curl_setopt_array($ch, $options);
                $rt = json_decode(curl_exec($ch), true);
                if ($rt['error']) {
                    // 错误日志
                    @file_put_contents(WRITEPATH.'bdts_log.php', date('Y-m-d H:i:s').' ['.$url.'] - 失败 - '.$rt['message'].PHP_EOL, FILE_APPEND);
                } else {
                    // 推送成功
                    @file_put_contents(WRITEPATH.'bdts_log.php', date('Y-m-d H:i:s').' ['.$url.'] - 成功'.PHP_EOL, FILE_APPEND);
                }
            }

        }

    }


}